﻿namespace ZGDisassembler.UI;

public partial class LabelLinks : Form
{
	public HashSet<string> FileSets { get; private set; } = new();
	public bool OK = false;

	public LabelLinks()
	{
		InitializeComponent();

		add_btn.Click += Add_btn_Click;
		del_btn.Click += Del_btn_Click;
		ok_btn.Click += Ok_btn_Click;
	}

	private void Add_btn_Click(object? sender, EventArgs e)
	{
		var ofd = new OpenFileDialog
		{
			Filter = "INI配置文件|*.ini",
			InitialDirectory = Path.Combine(AppContext.BaseDirectory, "labels")
		};
		ofd.FileOk += delegate
		{
			FileSets.Add(ofd.FileName);
			UpdateList();
		};
		ofd.ShowDialog(this);
	}

	private void Del_btn_Click(object? sender, EventArgs e)
	{
		if (file_list.SelectedIndex < 0)
			return;

		var filePath = (string)file_list.Items[file_list.SelectedIndex];
		file_list.Items.RemoveAt(file_list.SelectedIndex);
		FileSets.Remove(filePath);
	}

	private void Ok_btn_Click(object? sender, EventArgs e)
	{
		OK = true;
		Close();
	}

	public void SetFiles(string[] filePaths)
	{
		if (filePaths == null)
			return;

		foreach (var file in filePaths)
			FileSets.Add(file);

		UpdateList();
	}

	private void UpdateList()
	{
		file_list.Items.Clear();
		foreach (var file in FileSets)
			file_list.Items.Add(file);
	}
}
